from machine import Pin, I2C,RTC
import utime
import ntptime 
from machine import RTC
import network
import machine

rst=Pin(15,Pin.OUT)
pl=Pin(22,Pin.OUT)
pd=Pin(23,Pin.OUT)
i2c = I2C(scl=Pin(18), sda=Pin(19), freq=100000)
#i2c = I2C(1,freq=400000)

print(i2c.scan())
rst.value(0)
utime.sleep_ms(50)
rst.value(1)

def ds3231GetTime(buf):
    buf
    s=(buf[0]&0x0f)+(buf[0]>>4)*10
    m=(buf[1]&0x0f)+(buf[1]>>4)*10
    h=(buf[2]&0x0f)+(buf[2]>>4)*10
    return (h,m,s)
def ScreenInit():
    i2c.start()
    i2c.write('\x7e\x00\x3b\x03\x38\x05\x0c\x39\x08\x10\x90')
    i2c.stop()
def clearScreen():
    for i in range(0,8):
        i2c.start()
        i2c.write('\x7e\x00\x38')
        i2c.write((0x40+i).to_bytes(1,'big'))
        transfer(0xe4)
        transfer(0xf0)
        i2c.stop()
        i2c.start()
        transfer(0x7e)
        transfer(0x40)
        for n in range(0,132):
            transfer(0x00)
        i2c.stop()
def showtime__():
    while True:
        #print("A")
        #i2c.start()
        try:
            buf = i2c.readfrom_mem(104, 0x00,4)
        except:
            try:
                i2c.start()
                print("start")
            except:
                print("error")


        print(ds3231GetTime(buf))

        print("----")
        utime.sleep(1)
        

def transfer(data1):
    for i in range(0,8):
        pl.value(0)
        if (data1&0x80):
            pd.value(1)
        else:
            pd.value(0)
        pl.value(1)
        pl.value(0)
        data1=data1<<1
    pd.value(0)
    pl.value(1)
    pl.value(0)

ScreenInit()
clearScreen()


dp=[[0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0xC0,0x40,0x40,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0xF0,0xFE,0xFF,0x0F,0x01,0x00,0x00,0x00,0x01,0x07,0xFF,0xFE,0xF0,0x00,0x00,0x00,0x3F,0xFF,0xFF,0xC0,0x00,0x00,0x00,0x00,0x00,0x80,0xFF,0xFF,0x3F,0x00,0x00,0x00,0x00,0x01,0x03,0x07,0x0E,0x0C,0x08,0x08,0x0E,0x07,0x03,0x01,0x00,0x00],#00
[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x08,0x08,0x0C,0x0F,0x0F,0x0F,0x0C,0x08,0x08,0x08,0x00,0x00],#11
[0x00,0x00,0x00,0x00,0x80,0xC0,0x40,0x40,0x40,0x40,0xC0,0xC0,0x80,0x80,0x00,0x00,0x00,0x00,0x1E,0x1F,0x19,0x00,0x00,0x00,0x00,0x00,0x00,0xC1,0xFF,0xFF,0x3E,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0x70,0x38,0x1C,0x0E,0x07,0x03,0x01,0xC0,0xC0,0x00,0x00,0x00,0x0E,0x0F,0x0D,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0E,0x0F,0x03,0x00],#22
[0x00,0x00,0x00,0x80,0x80,0xC0,0x40,0x40,0x40,0xC0,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x0F,0x0F,0x00,0x00,0x00,0x00,0x80,0xC1,0xFF,0x7F,0x3E,0x00,0x00,0x00,0x00,0xC0,0xC0,0xC0,0x00,0x01,0x01,0x01,0x03,0x03,0x06,0xFE,0xFC,0xF0,0x00,0x00,0x00,0x03,0x07,0x07,0x0C,0x08,0x08,0x08,0x08,0x0C,0x06,0x07,0x03,0x00,0x00],#33
[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xE0,0x70,0x1C,0x0F,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x30,0x3C,0x2E,0x27,0x21,0x20,0x20,0x20,0xFF,0xFF,0xFF,0x20,0x20,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x08,0x08,0x0F,0x0F,0x0F,0x08,0x08,0x08,0x00],#44
[0x00,0x00,0x00,0x00,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0x00,0x00,0x00,0x00,0xFF,0xFF,0x80,0xC0,0x40,0x40,0x40,0xC0,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0xC0,0xC3,0xC3,0x01,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0xFF,0xFC,0x00,0x00,0x00,0x03,0x07,0x04,0x0C,0x08,0x08,0x08,0x08,0x0C,0x07,0x07,0x03,0x00,0x00],#55
[0x00,0x00,0x00,0x00,0x00,0x80,0x80,0xC0,0x40,0x40,0x40,0xC0,0x80,0x80,0x00,0x00,0x00,0x00,0xE0,0xFC,0xFF,0x87,0xC1,0xC0,0x40,0x40,0xC0,0xC3,0x83,0x03,0x00,0x00,0x00,0x00,0x7F,0xFF,0xFF,0x83,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0xFF,0xFC,0x00,0x00,0x00,0x00,0x01,0x07,0x07,0x0E,0x0C,0x08,0x08,0x0C,0x0E,0x07,0x03,0x00,0x00],#66
[0x00,0x00,0x00,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0x00,0x00,0x00,0x0E,0x0F,0x03,0x00,0x00,0x00,0x00,0xE0,0xF8,0x1E,0x07,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xFC,0xFF,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x0F,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00],#77
[0x00,0x00,0x00,0x00,0x80,0xC0,0xC0,0x40,0x40,0x40,0xC0,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0x3E,0x7F,0xFF,0xF1,0xC0,0xC0,0x80,0x00,0x80,0xC1,0x7F,0x7F,0x1E,0x00,0x00,0xF0,0xFC,0xFE,0x0E,0x03,0x01,0x01,0x03,0x07,0x0F,0x1E,0xFE,0xFC,0xF0,0x00,0x00,0x01,0x03,0x07,0x06,0x0C,0x08,0x08,0x08,0x08,0x0C,0x06,0x07,0x03,0x01,0x00],#88
[0x00,0x00,0x00,0x80,0x80,0xC0,0x40,0x40,0x40,0x40,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0xFC,0xFF,0xFF,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xFF,0xFE,0xF8,0x00,0x00,0x01,0x03,0x07,0x0F,0x0C,0x08,0x08,0x08,0x0C,0x06,0xE7,0xFF,0x7F,0x1F,0x00,0x00,0x00,0x07,0x07,0x0F,0x08,0x08,0x08,0x0C,0x0E,0x07,0x03,0x01,0x00,0x00,0x00],#99
[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xE0,0xE0,0xE0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x0F,0x0F,0x0F,0x06,0x00,0x00,0x00,0x00,0x00]]#:10

def print32_16(y,x,data1):
    x=(x-1)*16+4
    print(x)
    for i in range(0,4):
        i2c.start()
        i2c.write('\x7e\x00\x38')
        transfer(0x40+i+y)

        transfer(0xe0+(x&0x0f))
        transfer(0xf0+((x>>4)&0x0f))

        i2c.stop()
        i2c.start()
        i2c.write("\x7e")
        i2c.write('\x40')
        for n in range(0,16):
            transfer(data1[n+i*16])
        i2c.stop()

def showtime(times,f=True):
    if t[6]==0 or f :

        print32_16(0,8,dp[int(t[6]%10)])
        print32_16(0,7,dp[int(t[6]/10)])
        print32_16(0,5,dp[int(t[5]%10)])
        print32_16(0,4,dp[int(t[5]/10)])
        print32_16(0,2,dp[int(t[4]%10)])
        print32_16(0,1,dp[int(t[4]/10)])
        print32_16(0,3,dp[10])
        print32_16(0,6,dp[10])
        
        
    elif int(t[6]%10)==0 :
        print32_16(0,7,dp[int(t[6]/10)])
        print32_16(0,8,dp[int(t[6]%10)])
    else:
        print32_16(0,8,dp[int(t[6]%10)])
def sync_ntp():
    ntptime.NTP_DELTA = 3155644800   # 可选 UTC+8偏移时间（秒），不设置就是UTC0
    ntptime.host = 'ntp1.aliyun.com'  # 可选，ntp服务器，默认是"pool.ntp.org"
    try:
        ntptime.settime()   # 修改设备时间,到这就已经设置好了
    except:
        print("NTP Error")

sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
                          
#sta_if.connect("<AP_name>", "<password>") # Connect to an AP
while not sta_if.isconnected():
    sta_if.connect("Network_1", "15368274761")
    utime.sleep(1)


sync_ntp()
rtc = RTC()
t=rtc.datetime()
fl=True
while True:
    t=rtc.datetime()
    print("{0:0>2d}:{1:0>2d}:{2:0>2d}".format(t[4],t[5],t[6]))
    print(t)
    showtime(t,fl)
    print(int((1000000-t[7])/1000))
    utime.sleep_us(1000000-t[7])
    fl=False